package cn.uce.cc.srv.biz.impl;

import cn.uce.base.ccos.MsgCRMParam;
import cn.uce.base.ccos.WebPoint;
import cn.uce.cc.api.service.biz.ICcosOrgBiz;
import cn.uce.cc.api.service.biz.ICcosUserBiz;
import cn.uce.cc.srv.client.CcosInterfaceClient;
import cn.uce.cc.srv.client.JsonHttpClient;
import cn.uce.common.constants.PubApiURLConstants;
import cn.uce.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("ccosUserBiz")
public class CcosUserBiz implements ICcosUserBiz {
    private static final Logger log = LoggerFactory.getLogger(CcosUserBiz.class);

    @Resource
    CcosInterfaceClient ccosInterfaceClient;
    @Resource
    private PubApiURLConstants pubApiURLConstants;

    @Value("${spring.serviceName.queryRelatedNamesByuserCode}")
    private String queryRelatedNamesByuserCode;


    /**
     * 查询用户信息
     * @param empCode
     * @return
     */
    @Override
    public Map<String, Object> getUserInfo(String empCode) {
        /** 请求接口 */
        Map<String, Object> params = new HashMap<>();
        params.put("serviceName", queryRelatedNamesByuserCode);
        params.put("parameters", new Object[]{empCode});
        //TODO sit需要删除此代码
//        params.put("parameters", new Object[]{"000002@yimidida"});
        String result = null;
        String returnMessage = null;
        try {
//            log.warn("getUserInfo:begin:"+JSON.toJSONString(empCode));
//            returnMessage = JsonHttpClient.postJsonForOpenPlatBridge( JSON.toJSONString(params));
//            if(null == returnMessage){
//                return null;
//            }
//            log.warn("getUserInfobegin,"+returnMessage);
//            String resultmsg =  ccosInterfaceClient.queryOpenPlatFronBridge(returnMessage);
//            log.warn("getUserInfoend,"+resultmsg);
//            JSONObject resultMessage = JSONObject.parseObject(resultmsg);
//            if("true".equals(resultMessage.getString("success"))){
//                result = resultMessage.getString("result");
//            }
//            params.put("result",result);
//            JSONObject jsonObject= JSONObject.parseObject(result);
//            params.put("userName",jsonObject.getString("userName"));
//            params.put("compCode",jsonObject.getString("compCode"));
//            params.put("compLongName",jsonObject.getString("compLongName"));
//            params.put("siteCode",jsonObject.getString("siteCode"));
//            params.put("siteName",jsonObject.getString("siteName"));
            params.put("compCode","yimidida");
            params.put("compLongName","壹米滴答");
            params.put("siteCode","yimidida");
            params.put("siteName","壹米滴答供应链集团有限公司");
            params.put("userCode", empCode);
            log.warn("getUserInfo:result:"+JSON.toJSONString(params));
        } catch (Exception e) {
            log.warn("getUserInfo",e);
        }
        return params;
    }

    @Override
    public String getUserName(String userCode) {
        /** 请求接口 */
        Map<String, Object> params = new HashMap<>();
        params.put("serviceName", queryRelatedNamesByuserCode);
        params.put("parameters", new Object[]{userCode});
        return getResult(params);
    }

    private String getResult(Map<String, Object> params) {
        String result = "result";
        String msgTrue = "true";
        String success = "success";
        String returnMessage = null;
        try {
            returnMessage = JsonHttpClient.postJsonForOpenPlatBridge( JSON.toJSONString(params));
            if(null == returnMessage){
                return null;
            }
            log.warn("getUserInfobegin,"+returnMessage);
            String resultmsg =  ccosInterfaceClient.queryOpenPlatFronBridge(returnMessage);
            log.warn("getUserInfoend,"+resultmsg);
            JSONObject resultMessage = JSONObject.parseObject(resultmsg);
            if(msgTrue.equals(resultMessage.getString(success))){
                return resultMessage.getString(result);
            }
        }catch (Exception e){
            log.error("接口异常",e);
        }
        return null;
    }
}